﻿using RXY_Mall.Services.Order;
using System;
using System.ServiceProcess;
using System.Timers;

namespace RXY_Mall.WindowsService
{
    public partial class RxyServices : ServiceBase
    {
        public IOrderService orderService;
        public RxyServices()
        {
            InitializeComponent();
            orderService = new OrderService();
        }

        protected override void OnStart(string[] args)
        {
            PlanSyncOrder();
            PlanCancelOrder();
        }

        protected override void OnStop()
        {
        }

        /// <summary>
        /// 计划-同步订单
        /// </summary>
        public void PlanSyncOrder()
        {
            ExecuteSyncOrder(null, null);
            Timer timer = new Timer(1000 * 60 * 1); //间隔10分钟
            timer.AutoReset = true;
            timer.Enabled = true;  //执行一次
            timer.Elapsed += new ElapsedEventHandler(ExecuteSyncOrder);
            timer.Start();
        }

        /// <summary>
        /// 执行-同步订单
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public void ExecuteSyncOrder(object source, System.Timers.ElapsedEventArgs e)
        {
            orderService.SyncAddOrder();
        }

        /// <summary>
        /// 计划-同步订单
        /// </summary>
        public void PlanCancelOrder()
        {
            ExecuteCancelOrder(null, null);
            Timer timer = new Timer(1000 * 60 * 1); //间隔10分钟
            timer.AutoReset = true;
            timer.Enabled = true;  //执行一次
            timer.Elapsed += new ElapsedEventHandler(ExecuteCancelOrder);
            timer.Start();
        }

        /// <summary>
        /// 执行-取消订单
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public void ExecuteCancelOrder(object source, System.Timers.ElapsedEventArgs e)
        {
            orderService.SyncCancelOrder();
        }


    }
}
